{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "634165f9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting run.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile run.py\n",
    "'''\n",
    "Author: '遂季琉璃' 1846621493@qq.com\n",
    "Date: 2025-07-29 20:43:09\n",
    "LastEditors: '遂季琉璃' 1846621493@qq.com\n",
    "LastEditTime: 2025-07-29 22:36:23\n",
    "FilePath: \\jqktrader-main\\构建远端服务.py\n",
    "Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE\n",
    "'''\n",
    "import jqktrader\n",
    "from jqktrader.log import logger\n",
    "user = jqktrader.use()\n",
    "user.connect(\n",
    "  exe_path=r'C:\\同花顺软件\\同花顺\\xiadan.exe',\n",
    "  tesseract_cmd=r'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'\n",
    ")\n",
    "\n",
    "import flask\n",
    "import json\n",
    "app = flask.Flask(__name__)\n",
    "\n",
    "# 获取资金状况\n",
    "@app.route('/balance', methods=['GET'])\n",
    "def balance():\n",
    "    \"\"\"获取资金信息\"\"\"\n",
    "    result = user.balance\n",
    "    logger.info(\"flask收到资金查询请求，返回：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 获取持仓\n",
    "@app.route('/position', methods=['GET'])\n",
    "def position():\n",
    "    \"\"\"获取持仓信息\"\"\"\n",
    "    result = user.position\n",
    "    logger.info(\"flask收到持仓查询请求，返回：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 获取当日委托\n",
    "@app.route('/today_entrusts', methods=['GET'])\n",
    "def today_entrusts():\n",
    "    \"\"\"获取当日委托信息\"\"\"\n",
    "    result = user.today_entrusts\n",
    "    logger.info(\"flask收到当日委托查询请求，返回：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 获取当日成交\n",
    "@app.route('/today_trades', methods=['GET'])\n",
    "def today_trades():\n",
    "    \"\"\"获取当日成交信息\"\"\"\n",
    "    result = user.today_trades\n",
    "    logger.info(\"flask收到当日成交查询请求，返回：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 获取可撤单列表\n",
    "@app.route('/cancel_entrusts', methods=['GET'])\n",
    "def cancel_entrusts():\n",
    "    \"\"\"获取可撤单委托列表\"\"\"\n",
    "    result = user.cancel_entrusts\n",
    "    logger.info(\"flask收到可撤单列表查询请求，返回：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 撤单\n",
    "@app.route('/cancel', methods=['POST'])\n",
    "def cancel():\n",
    "    \"\"\"撤销指定委托单\"\"\"\n",
    "    entrust_no = flask.request.form.get('entrust_no')\n",
    "    logger.info(\"flask收到撤单请求：{}\".format(json.dumps({'entrust_no': entrust_no}, ensure_ascii=False)))\n",
    "    result = user.cancel_entrust(entrust_no)\n",
    "    logger.info(\"撤单结果：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 批量撤单\n",
    "@app.route('/cancel_all', methods=['POST'])\n",
    "def cancel_all():\n",
    "    \"\"\"批量撤销所有可撤单委托\"\"\"\n",
    "    logger.info(\"flask收到批量撤单请求\")\n",
    "    user.cancel_all_entrusts()\n",
    "    return flask.jsonify({'message': '已执行批量撤单'})\n",
    "\n",
    "# 买入\n",
    "@app.route('/buy', methods=['POST'])\n",
    "def buy():\n",
    "    \"\"\"买入股票\"\"\"\n",
    "    code = flask.request.form.get('code')\n",
    "    price = flask.request.form.get('price')\n",
    "    amount = flask.request.form.get('amount')\n",
    "    logger.info(\"flask收到买入请求：{}\".format(json.dumps({'code': code, 'price': price, 'amount': amount}, ensure_ascii=False)))\n",
    "    result = user.buy(code, price, amount)\n",
    "    logger.info(\"买入结果：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 卖出\n",
    "@app.route('/sell', methods=['POST'])\n",
    "def sell():\n",
    "    \"\"\"卖出股票\"\"\"\n",
    "    code = flask.request.form.get('code')\n",
    "    price = flask.request.form.get('price')\n",
    "    amount = flask.request.form.get('amount')\n",
    "    logger.info(\"flask收到卖出请求：{}\".format(json.dumps({'code': code, 'price': price, 'amount': amount}, ensure_ascii=False)))\n",
    "    result = user.sell(code, price, amount)\n",
    "    logger.info(\"卖出结果：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 市价买入\n",
    "@app.route('/market_buy', methods=['POST'])\n",
    "def market_buy():\n",
    "    \"\"\"市价买入股票\"\"\"\n",
    "    code = flask.request.form.get('code')\n",
    "    amount = flask.request.form.get('amount')\n",
    "    logger.info(\"flask收到市价买入请求：{}\".format(json.dumps({'code': code, 'amount': amount}, ensure_ascii=False)))\n",
    "    result = user.market_buy(code, amount, ttype='本方最优')\n",
    "    logger.info(\"市价买入结果：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 市价卖出\n",
    "@app.route('/market_sell', methods=['POST'])\n",
    "def market_sell():\n",
    "    \"\"\"市价卖出股票\"\"\"\n",
    "    code = flask.request.form.get('code')\n",
    "    amount = flask.request.form.get('amount')\n",
    "    logger.info(\"flask收到市价卖出请求：{}\".format(json.dumps({'code': code, 'amount': amount}, ensure_ascii=False)))\n",
    "    result = user.market_sell(code, amount, ttype='本方最优')\n",
    "    logger.info(\"市价卖出结果：{}\".format(json.dumps(result, ensure_ascii=False)))\n",
    "    return flask.jsonify(result)\n",
    "\n",
    "# 刷新数据\n",
    "@app.route('/refresh', methods=['POST'])\n",
    "def refresh():\n",
    "    \"\"\"刷新客户端数据\"\"\"\n",
    "    logger.info(\"flask收到刷新请求\")\n",
    "    user.refresh()\n",
    "    return flask.jsonify({'message': '刷新成功'})\n",
    "\n",
    "# 退出客户端\n",
    "@app.route('/exit', methods=['POST'])\n",
    "def exit_client():\n",
    "    \"\"\"退出客户端\"\"\"\n",
    "    logger.info(\"flask收到退出请求\")\n",
    "    user.exit()\n",
    "    return flask.jsonify({'message': '客户端已退出'})\n",
    "\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "  app.run(host='0.0.0.0', port=6030)\n",
    "\n",
    "\n",
    "\n",
    "# 单元测试\n",
    "# curl -X POST -d \"code=000878&price=12.45&amount=100\" http://192.168.5.7:6030/buy\n",
    "# curl -X POST -d \"entrust_no=4723953366\" http://192.168.5.7:6030/cancel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a64fff69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (ths_jy)",
   "language": "python",
   "name": "ths_jy"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
